﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using Microsoft.JCI.Common.Entity;
using System.Collections.Generic;
using Microsoft.JCI.Bll;
using Microsoft.JCI.Common.Utility;
using Microsoft.JCI.Portal.UserControl; 

namespace Microsoft.JCI.Portal.Agent
{
    public partial class CallbackInfo : BasePage
    {
        public Guid DispatchFormID
        {
            get
            {
                if (ViewState["DispatchFormID"] == null) return Guid.Empty;
                else return new Guid(ViewState["DispatchFormID"].ToString());
            }
            set
            {
                ViewState["DispatchFormID"] = value.ToString();
            }
        }

        public Guid EvaluationID
        {
            get
            {
                if (ViewState["EvaluationID"] == null) return Guid.Empty;
                else return new Guid(ViewState["EvaluationID"].ToString());
            }
            set { ViewState["EvaluationID"] = value.ToString(); }
        }

        public Guid CustomerID
        {
            get
            {
                if (ViewState["CustomerID"] == null) return Guid.Empty;
                else return new Guid(ViewState["CustomerID"].ToString());
            }
            set { ViewState["CustomerID"] = value.ToString(); }
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                InitPage();

                if (Request.QueryString["DispatchFormID"] != null)
                {
                    DispatchFormID = new Guid(Request.QueryString["DispatchFormID"]);  

                    CustomerID = new SystemBll().Get<DisDispatchFormEntity>(DispatchFormID).CustomerId;
                    ucCustomerCtrlInfo.CustomerID = CustomerID;
                                   
                    LoadData();
                }       
                       
                if (rblFormCategory.SelectedValue != string.Empty)
                {
                    LoadFormType(Convert.ToInt32(rblFormCategory.SelectedValue));
                }
            }
        }   
      
        private void InitPage()
        {
            EnumeratorHelper[] enums = EnumeratorHelper.GetFieldTexts(typeof(Enumerator.FormCategory));
            foreach (EnumeratorHelper e in enums)
            {
                rblFormCategory.Items.Add(new ListItem(e.EnumDisplayText, e.EnumValue.ToString()));
            }

            rblFormCategory.SelectedValue = ((int)Enumerator.FormCategory.Maintain).ToString();

            //用户控件
            SystemBll bll = new SystemBll();
            ddlM_IssueSource.DataSource = bll.GetDictionaryListFromCache(Microsoft.JCI.Common.Utility.Enumerator.DictionaryType.MaintainIssueSource);
            ddlM_IssueSource.DataTextField = "Value";
            ddlM_IssueSource.DataValueField = "DictionaryID";
            ddlM_IssueSource.DataBind();

            ddlM_MaintainType.DataSource = bll.GetDictionaryListFromCache(Microsoft.JCI.Common.Utility.Enumerator.DictionaryType.MaintainType);
            ddlM_MaintainType.DataTextField = "Value";
            ddlM_MaintainType.DataValueField = "DictionaryID";
            ddlM_MaintainType.DataBind();

            pnlFormTypeMaintain.Visible = false;
            pnlFormTypeComplaint.Visible = false;
            pnlFormTypeAdvisory.Visible = false;
        }

        private void LoadData()
        {
            if (DispatchFormID != Guid.Empty)
            {
                SystemBll bll = new SystemBll();
                DisDispatchFormEntity FormEntity = bll.Get<DisDispatchFormEntity>(DispatchFormID);
                if (FormEntity != null)
                {
                    //加载客户信息
                    ucCustomerCtrlInfo.CustomerID = FormEntity.CustomerId;
                    CustomerID = FormEntity.CustomerId;

                    //加载产品信息
                    ProductCtrl1.ProductID = FormEntity.ProductID;
                    ProductCtrl1.LoadProduct();

                    //加载派工单信息
                    rblFormCategory.SelectedValue = FormEntity.FormCategory.ToString();
                    if (FormEntity.M_IssueSource != Guid.Empty)
                        ddlM_IssueSource.SelectedValue = FormEntity.M_IssueSource.ToString();

                    if (FormEntity.M_MaintainType != null)
                        ddlM_MaintainType.SelectedValue = FormEntity.M_MaintainType.ToString();

                    ddlM_Priority.SelectedValue = FormEntity.M_Priority;
                    txtM_PersonalityService.Text = FormEntity.M_PersonalityService;
                    txtM_HQappoint.Text = FormEntity.M_HQappoint;
                    txtM_RepairContent.Text = FormEntity.M_RepairContent;
                    txtM_Remark.Text = FormEntity.M_Remark;
                    txtComplaintDesc.Text = FormEntity.C_Description;
                    txtAdvisoryDesc.Text = FormEntity.A_Description;
                    DisEvaluationEntity disEvaluationEntity = new SystemBll().Get<DisEvaluationEntity>(" and DispatchFormId = '" + DispatchFormID.ToString() + "'");
                    if (disEvaluationEntity != null)
                    {
                        EvaluationID = disEvaluationEntity.EvaluationID;
                        txtAccessory.Text = disEvaluationEntity.Accessory;

                        ddlDispatchTimelyScore.SelectedValue = disEvaluationEntity.DispatchTimelyScore.ToString();
                        txtFee.Value = System.Convert.ToDouble(disEvaluationEntity.Fee);
                        chkHasChangeAccessory.Checked = disEvaluationEntity.HasChangeAccessory;
                        chkHasReceiveFee.Checked = disEvaluationEntity.HasReceiveFee;
                        ddlServiceProviderAttitudeScore.SelectedValue = disEvaluationEntity.ServiceProviderAttitudeScore.ToString();
                        ddlServiceStandard.SelectedValue = disEvaluationEntity.ServiceStandard.ToString();
                        txtSummary.Text = disEvaluationEntity.Summary;
                        ddlTechnologyScore.SelectedValue = disEvaluationEntity.TechnologyScore.ToString();
                        ddlVisitTimelyScore.SelectedValue = disEvaluationEntity.VisitTimelyScore.ToString();
                    }
                }
            }
        }

        private void LoadFormType(int Type)
        {
            if (Type == (int)Enumerator.FormCategory.Maintain)
            {
                pnlFormTypeMaintain.Visible = true;
                pnlFormTypeComplaint.Visible = false;
                pnlFormTypeAdvisory.Visible = false;
            }
            else if (Type == (int)Enumerator.FormCategory.Complaint)
            {
                pnlFormTypeMaintain.Visible = false;
                pnlFormTypeComplaint.Visible = true;
                pnlFormTypeAdvisory.Visible = false;
            }
            else if (Type == (int)Enumerator.FormCategory.Advisory)
            {
                pnlFormTypeMaintain.Visible = false;
                pnlFormTypeComplaint.Visible = false;
                pnlFormTypeAdvisory.Visible = true;
            }
        }
     

        protected void radgridFormHistoryList_ItemDataBound(object sender, Telerik.WebControls.GridItemEventArgs e)
        {
            if (e.Item.ItemType == Telerik.WebControls.GridItemType.Item || e.Item.ItemType == Telerik.WebControls.GridItemType.AlternatingItem)
            {
                LinkButton lnkbtnViewDispatch = e.Item.FindControl("lnkbtnViewDispatch") as LinkButton;
                if (lnkbtnViewDispatch != null)
                {
                    lnkbtnViewDispatch.Attributes.Add("onclick", "openMyFullWin('AgentDispatchOrderView.aspx?DispatchFormId=" + e.Item.OwnerTableView.DataKeyValues[e.Item.ItemIndex]["DispatchFormId"].ToString() + "','GendDispatchOrderView');");
                }

                Label lblFormCategory = e.Item.FindControl("lblFormCategory") as Label;

                DisDispatchFormEntity entity = e.Item.DataItem as DisDispatchFormEntity;
                if (entity != null)
                {
                    lblFormCategory.Text = EnumeratorHelper.GetFieldText((Enumerator.FormCategory)entity.FormCategory);
                }

            }
        }

        protected void rblFormCategory_SelectedIndexChanged(object sender, EventArgs e)
        {
            LoadFormType(Convert.ToInt32(rblFormCategory.SelectedValue));
        }

        private void Save()
        {
            DisEvaluationEntity entity = new DisEvaluationEntity();
            if (EvaluationID != Guid.Empty)
            {
                entity.EvaluationID = EvaluationID;
            }
            else
            {
                entity.EvaluationID = Guid.NewGuid();
            }
            entity.Accessory = txtAccessory.Text.Trim();
            entity.DispatchFormId = DispatchFormID;
            entity.DispatchTimelyScore = System.Convert.ToInt32(ddlDispatchTimelyScore.SelectedValue);
            entity.Fee = System.Convert.ToDecimal(txtFee.Value);
            entity.HasChangeAccessory = chkHasChangeAccessory.Checked;
            entity.HasReceiveFee = chkHasReceiveFee.Checked;
            entity.ServiceProviderAttitudeScore = System.Convert.ToInt32(ddlServiceProviderAttitudeScore.SelectedValue);
            entity.ServiceStandard = System.Convert.ToInt32(ddlServiceStandard.SelectedValue);
            entity.Summary = txtSummary.Text.Trim();
            entity.TechnologyScore = System.Convert.ToInt32(ddlTechnologyScore.SelectedValue);
            entity.VisitTimelyScore = System.Convert.ToInt32(ddlVisitTimelyScore.SelectedValue);
            new DisEvaluationBll().InsertOrUpdate(entity);
           DisDispatchFormEntity disEntity =  new SystemBll().Get<DisDispatchFormEntity>(DispatchFormID);
           disEntity.FormStatus = System.Convert.ToInt32(Enumerator.FormStatus.Visited);
           new SystemBll().Update(disEntity);
            EvaluationID = entity.EvaluationID;
        }

        protected void lnkbtnSaveAndClose_Click(object sender, EventArgs e)
        {
            Save();
            ClientScript.RegisterStartupScript(this.GetType(), "saveandclose", "window.close();", true);
        }                   
    }
}
